package com.hsmpay.yl8583.utils;

import java.nio.ByteBuffer;

public abstract class PackageUtil {
	
	public static byte[] getXORContent( byte[] content )
	{
	   int  uiOffset=0, i=0;
	   byte []sOutMAC =new byte[8];
	   while( content.length > uiOffset )
	   {
	     if( content.length - uiOffset <= 8 )
	     {
			for( i=0; i<content.length - uiOffset; i++)
				sOutMAC[i] ^= content[uiOffset + i];
			break;
	     }
		for( i=0; i<8; i++)
			sOutMAC[i] ^= content[uiOffset + i];
		uiOffset += 8;
	   }
	   return sOutMAC; 
	}
	//ByteToHexString
	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1)
				hs = hs + "0" + stmp;
			else
				hs = hs + stmp;
		}
		return hs.toUpperCase();
	}
	//HexStringToByte
	public static byte[] hexStringToByte(String hex) {
		hex=hex.toUpperCase();
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}
	
	private static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		return b;
	}
		
	private static byte abcd_to_asc(byte abyte)
	{
		if (abyte <= 9)
			abyte = (byte) (abyte + '0');
		else
			abyte = (byte) (abyte + 'A' - 10);
		return (abyte);
	}

	public static byte[] BCD2ASCII(byte [] bcd_buf,int len)
	{
		int i,n;
		n=len;
		ByteBuffer asc_buf=ByteBuffer.allocate(n);
		byte tmp;
		for (i = 0; i < n/2; i++) {
			tmp = (byte) ((bcd_buf[i] & 0xf0) >> 4);
			tmp = abcd_to_asc(tmp);
			asc_buf.put(tmp);
			tmp = (byte) (bcd_buf[i] & 0x0f);
			tmp = abcd_to_asc(tmp);
			asc_buf.put(tmp);
		}
		if (n % 2!=0) {
			tmp = (byte) ((bcd_buf[i] & 0xf0) >> 4);
			tmp = abcd_to_asc(tmp);
			asc_buf.put(tmp);
		}
		asc_buf.flip();
	    byte[] res=null;
    	res=new byte[asc_buf.remaining()];
    	asc_buf.get(res, 0, res.length);
    	return res;
	}

	private static byte aasc_to_bcd(byte asc)
	{
		byte bcd;

		if ((asc >= '0') && (asc <= '9'))
			bcd = (byte) (asc - '0');
		else if ((asc >= 'A') && (asc <= 'F'))
			bcd = (byte) (asc - 'A' + 10);
		else if ((asc >= 'a') && (asc <= 'f'))
			bcd = (byte) (asc - 'a' + 10);
		else if ((asc > 0x39) && (asc <= 0x3f))
			bcd = (byte) (asc - '0');
		else {
			/*		printf( "\f[Warning] : Bad HEX digid" );
					bcd = 0;
			*/ 		
			bcd = 0x0f;
		}
		return bcd;
	}
	//ASCII2BCD	
	public static byte[] ASCII2BCD(byte[] asc_buf)
	{
		int j=0;
		int n=asc_buf.length;
		byte []bcd_buf=new byte[(n+1)/2];
		for (int i = 0; i < (n+1)/2; i++) {
			bcd_buf[i] = aasc_to_bcd(asc_buf[j++]);
			bcd_buf[i] = (byte) (((j >= n) ? 0x00 : aasc_to_bcd(asc_buf[j++]))+ (bcd_buf[i] << 4));
		}
		return bcd_buf;
	}
	
	public static byte[] INT2BCD(int i,int lvar) throws Exception{
		return ASCII2BCD(String.format("%1$0"+lvar*2+"d",i).getBytes("US-ASCII"));
	}
	//将字符串转为ASCII�?
	public static String getCnASCII(String cnStr)
	{
		StringBuffer strBuf=new StringBuffer();
		//将字符串转换成字节序�?
		byte[] bGBK=cnStr.getBytes();
		for(int i=0;i<bGBK.length;i++)
		{
			//将每个字符转换成ASCII�?
			strBuf.append(Integer.toHexString(bGBK[i] & 0xff));
			//log.debug(Integer.toHexString(bGBK[i] & 0xff)); 
		}
		return strBuf.toString();
	}
	//转化十六进制编码ASCII为字符串 
	public static String toStringHex(String s) 
	{
		byte[] baKeyword = new byte[s.length()/2]; 
		for(int i = 0; i < baKeyword.length; i++) 
		{ 
			try 
			{ 
				baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16)); 
			} 
			catch(Exception e) 
			{ 
				e.printStackTrace(); 
			} 
		} 
		try 
		{ 
			s = new String(baKeyword, "utf-8");//UTF-16le:Not 
		}catch (Exception e1) 
		{ 
			e1.printStackTrace(); 
		} 
		return s; 
	} 
	
	/**
	 * 十进制 转 十六进制 
	 * @param x
	 * @return
	 */
	public static String intTohexStr(int x){
		String r = Integer.toHexString(x);
		if(x <= 16) r = "0"+r;
		return r;
	}
}

